import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

/** LinkGraphic Class
    CS158A Spring 2010 Project #2
    Graphic for a point-to-point link.
    @author Clare Lanam & Loc Phan
    */

public class LinkGraphic
{
   /** bridge at one end of the link */
   private BridgeIcon bridgeIconA;
   /** bridge at the other end of the link */
   private BridgeIcon bridgeIconB;
   
   // ------[GRAPHICS STUFF]------ //
   
   /** line representing the link */
   private Line2D.Float line;
   /** active link color */
   private static final Color COLOR_LINK = new Color(60, 60, 60);
   /** alpha value for transparency */
   private int alpha = 255;
   
   // ------[CONSTRUCTOR]------ //
   
   /** Constructor creates a new link associated with this graphic,
       linking the two bridges supplied in the parameters.
       @param a bridge on one end of the link
       @param b bridge on the other end of the link
       @param linkCost cost of using this link
       */
   
   public LinkGraphic(BridgeIcon a, BridgeIcon b, int linkCost)
   {
      bridgeIconA = a;
      bridgeIconB = b;
      Bridge.link(bridgeIconA.getBridge(), bridgeIconB.getBridge(), linkCost);
      line = new Line2D.Float(bridgeIconA.getX(), bridgeIconA.getY(),
            bridgeIconB.getX(), bridgeIconB.getY());
   }
   
   // ------[ACCESSORS]------ //
   
   /** Returns the alpha value. */
   public int getAlpha() { return alpha; }
   
   /** Returns the first bridge icon. */
   public BridgeIcon getBridgeIconA() { return bridgeIconA; }
   
   /** Returns the second bridge icon. */
   public BridgeIcon getBridgeIconB() { return bridgeIconB; }
   
   // ------[PUBLIC METHODS]------ //
   
   /** Destructor.  Call before deleting LinkGraphic to ensure
       that link is truly removed from bridges.
       */
   
   public void destructor()
   {
      Bridge.unlink(bridgeIconA.getBridge(), bridgeIconB.getBridge());
   }
   
   /** Handles drawing the link. ^___^
       @param g2 the graphics context
       */
   
   public void draw(Graphics2D g2)
   {
      Color c = new Color(COLOR_LINK.getRed(), COLOR_LINK.getGreen(),
            COLOR_LINK.getBlue(), alpha);
      
      g2.setColor(c);
      g2.setStroke(new BasicStroke(1));
      g2.draw(line);
   }
   
   /** Adds a value to transparency alpha. Caps btwn 255 and 0. */
   
   public void incrementAlpha(int inc)
   {
      alpha += inc;
      
      if (alpha > 255)
         alpha = 255;
      if (alpha < 0)
         alpha = 0;
   }
   
   /** Returns whether or not this link is between
       two specified BridgeIcons. */
   
   public boolean isLinkBetween(BridgeIcon a, BridgeIcon b)
   {
      return (bridgeIconA == a && bridgeIconB == b)
            || (bridgeIconA == b && bridgeIconB == a);
   }
   
   /** Returns whether or not this link touches a bridge. */
   
   public boolean isLinkTo(BridgeIcon b)
   {
      return (bridgeIconA == b || bridgeIconB == b);
   }
}
